#include <iostream>
#include <vector>
#include <filesystem>
#include <fstream>
#include "./cmdline.h"
#include "./utils/strutil.h"
namespace fs = std::filesystem;
int main(int argc, char *argv[])
{
    std::vector<std::string> args;
    for (int i = 0; i < argc; i++)
    {
        args.push_back(std::string(argv[i]));
    }
    cmdline::parser par;
    par.add<std::string>("file", 'f', "需要处理的文件路径", true);
    par.parse_check(args); // 解析参数
    auto &fileStr = par.get<std::string>("file");
    fs::path filePath = fileStr;
    if (!fs::exists(filePath))
    {
        std::cout << "文件不存在" << std::endl;
        return 1;
    }
    std::ifstream inputFile(filePath);
    if (!inputFile.is_open())
    {
        std::cout << "无法打开文件" << std::endl;
        return 1;
    }
    std::vector<std::string> sortedLines;
    std::string line;
    while (std::getline(inputFile, line))
    {
        if (isBlank(line)) // 跳过空白行
        {
            continue;
        }
        sortedLines.push_back(line);
    }
    inputFile.close();

    // 对sortedLines数组进行排序
    std::sort(sortedLines.begin(), sortedLines.end());
    std::ofstream outputFile(filePath);
    if (!outputFile.is_open())
    {
        std::cout << "无法写入文件" << std::endl;
        return 1;
    }

    for (const auto &sortedLine : sortedLines)
    {
        outputFile << sortedLine << std::endl;
    }
    outputFile.close();

    std::cout << "文件处理完成" << std::endl;
    return 0;
}